#include <gtest/gtest.h>
#include "order_manager.h"

using namespace hft;

class OrderManagerTest : public ::testing::Test {
protected:
    void SetUp() override {
        // 设置测试环境
        order_manager_ = std::make_unique<OrderManager>();
    }

    std::unique_ptr<OrderManager> order_manager_;
};

// 测试订单创建
TEST_F(OrderManagerTest, CreateOrder) {
    Order order;
    order.symbol = "IF2403";
    order.side = OrderSide::Buy;
    order.type = OrderType::Limit;
    order.price = 7500.0;
    order.volume = 10;
    order.account = "test_account";
    order.strategy_id = "test_strategy";

    std::string order_id = order_manager_->createOrder(order);
    ASSERT_FALSE(order_id.empty());

    Order* created_order = order_manager_->getOrder(order_id);
    ASSERT_NE(created_order, nullptr);
    EXPECT_EQ(created_order->symbol, "IF2403");
    EXPECT_EQ(created_order->side, OrderSide::Buy);
    EXPECT_EQ(created_order->price, 7500.0);
    EXPECT_EQ(created_order->volume, 10);
    EXPECT_EQ(created_order->status, OrderStatus::Created);
}

// 测试订单修改
TEST_F(OrderManagerTest, ModifyOrder) {
    Order order;
    order.symbol = "IF2403";
    order.side = OrderSide::Buy;
    order.type = OrderType::Limit;
    order.price = 7500.0;
    order.volume = 10;
    order.account = "test_account";
    order.strategy_id = "test_strategy";

    std::string order_id = order_manager_->createOrder(order);
    ASSERT_FALSE(order_id.empty());

    bool modified = order_manager_->modifyOrder(order_id, 7600.0, 5);
    EXPECT_TRUE(modified);

    Order* modified_order = order_manager_->getOrder(order_id);
    ASSERT_NE(modified_order, nullptr);
    EXPECT_EQ(modified_order->price, 7600.0);
    EXPECT_EQ(modified_order->volume, 5);
}

// 测试订单取消
TEST_F(OrderManagerTest, CancelOrder) {
    Order order;
    order.symbol = "IF2403";
    order.side = OrderSide::Buy;
    order.type = OrderType::Limit;
    order.price = 7500.0;
    order.volume = 10;
    order.account = "test_account";
    order.strategy_id = "test_strategy";

    std::string order_id = order_manager_->createOrder(order);
    ASSERT_FALSE(order_id.empty());

    bool canceled = order_manager_->cancelOrder(order_id);
    EXPECT_TRUE(canceled);

    Order* canceled_order = order_manager_->getOrder(order_id);
    ASSERT_NE(canceled_order, nullptr);
    EXPECT_EQ(canceled_order->status, OrderStatus::Canceled);
}

// 测试订单成交
TEST_F(OrderManagerTest, OrderFill) {
    Order order;
    order.symbol = "IF2403";
    order.side = OrderSide::Buy;
    order.type = OrderType::Limit;
    order.price = 7500.0;
    order.volume = 10;
    order.account = "test_account";
    order.strategy_id = "test_strategy";

    std::string order_id = order_manager_->createOrder(order);
    ASSERT_FALSE(order_id.empty());

    Trade trade;
    trade.trade_id = "T1";
    trade.order_id = order_id;
    trade.symbol = "IF2403";
    trade.side = OrderSide::Buy;
    trade.price = 7500.0;
    trade.volume = 5;
    trade.account = "test_account";
    trade.strategy_id = "test_strategy";

    order_manager_->onTrade(trade);

    Order* filled_order = order_manager_->getOrder(order_id);
    ASSERT_NE(filled_order, nullptr);
    EXPECT_EQ(filled_order->filled_volume, 5);
    EXPECT_EQ(filled_order->status, OrderStatus::PartialFilled);

    // 完全成交
    trade.trade_id = "T2";
    trade.volume = 5;
    order_manager_->onTrade(trade);

    filled_order = order_manager_->getOrder(order_id);
    ASSERT_NE(filled_order, nullptr);
    EXPECT_EQ(filled_order->filled_volume, 10);
    EXPECT_EQ(filled_order->status, OrderStatus::Filled);
}

// 测试查询功能
TEST_F(OrderManagerTest, QueryOrders) {
    // 创建多个订单
    Order order1;
    order1.symbol = "IF2403";
    order1.side = OrderSide::Buy;
    order1.type = OrderType::Limit;
    order1.price = 7500.0;
    order1.volume = 10;
    order1.account = "account1";
    order1.strategy_id = "strategy1";

    Order order2;
    order2.symbol = "IC2403";
    order2.side = OrderSide::Sell;
    order2.type = OrderType::Limit;
    order2.price = 8500.0;
    order2.volume = 5;
    order2.account = "account1";
    order2.strategy_id = "strategy2";

    Order order3;
    order3.symbol = "IF2403";
    order3.side = OrderSide::Buy;
    order3.type = OrderType::Limit;
    order3.price = 7600.0;
    order3.volume = 8;
    order3.account = "account2";
    order3.strategy_id = "strategy1";

    std::string order_id1 = order_manager_->createOrder(order1);
    std::string order_id2 = order_manager_->createOrder(order2);
    std::string order_id3 = order_manager_->createOrder(order3);

    // 测试按账户查询
    auto account1_orders = order_manager_->getAccountOrders("account1");
    EXPECT_EQ(account1_orders.size(), 2);

    // 测试按策略查询
    auto strategy1_orders = order_manager_->getStrategyOrders("strategy1");
    EXPECT_EQ(strategy1_orders.size(), 2);

    // 测试按合约查询
    auto if2403_orders = order_manager_->getSymbolOrders("IF2403");
    EXPECT_EQ(if2403_orders.size(), 2);

    // 测试活跃订单查询
    auto active_orders = order_manager_->getActiveOrders();
    EXPECT_EQ(active_orders.size(), 3);

    // 取消一个订单后再查询
    order_manager_->cancelOrder(order_id1);
    active_orders = order_manager_->getActiveOrders();
    EXPECT_EQ(active_orders.size(), 2);
}